//
//  HBPickerAlter.m
//  HeartBeat
//
//  Created by karong on 2023/9/5.
//

#import "HBPickerAlter.h"
#import "HBFeedbackCell.h"
#define CellH 44
@interface HBPickerAlter ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *listHeight;

@end

@implementation HBPickerAlter

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoyeView];
    [self layoutHeight];
}

- (void)layoyeView {
    self.titleLabel.text = self.titleStr;
}

- (void)layoutHeight {
    CGFloat height = MIN(self.dataArray.count, 5) * CellH;
    self.listHeight.constant = height;
    self.listView.scrollEnabled = self.dataArray.count > 5;
    [self.listView uploadLayout];
}

- (IBAction)closeAction:(id)sender {
    [self dismiss];
}

#pragma mark UIPickerViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"kcell"];
    UILabel * label = [cell viewWithTag:101];
    id object = self.dataArray[indexPath.row];
    if ([object isKindOfClass:NSClassFromString(@"NSString")]) {
        label.text = (NSString *)object;
    }
    else if ([self.delegate respondsToSelector:@selector(pickerAlterController:stringForRow:object:)]) {
        label.text = [self.delegate pickerAlterController:self stringForRow:indexPath.row object:object];
    }
    else if ([object isKindOfClass:NSClassFromString(@"GRFeedbackType")]) {
        label.text = ((GRFeedbackType *)object).name;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.selectHandle) {
            self.selectHandle(self.dataArray[indexPath.row]);
        }
    }];
}

@end
